<include file="public/header"/>

<body>

<!--main-->
<div class="pPersonal-warp">
  <form id="recharge">
    <div class="pdata-column-editor">
      <h4 class="tit">充值金额</h4>
      <input type="number" class="price-inpt" placeholder="请输入需要充值的金额" name="amount">
    </div>
  </form>
  <div class="pdata-column-save">
    <a href="javascript:;" class="s-btn" onclick="recharge()">确认充值</a>
    <a href="{:U('User/center')}" class="s-btn" style="margin-top: 10px;background: #fe7200">返回个人中心</a>
  </div>
</div>
<!--main End-->
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<script>
    var before_request=1;
    function recharge() {
        if(before_request == 0)
            return false;
        before_request = 0;
        $.ajax({
            type : "POST",
            url: '/Home/Order/recharge.html',
            data : $('#recharge').serialize(),// 你的formid
            dataType: 'json',
            success: function(result) {
                before_request = 1;
                if (result.status == 200){
                    layer.msg(result.msg,{icon:1,time:1000},function () {
                        var itemid = result.result;
                        $.ajax({
                            type : "POST",
                            url:"/Home/Payment/pay_recharge_order.html",
                            data : {itemid:itemid},//
                            dataType:'json',
                            success: function (data) {
                                if (data.status == 200) {
                                    function onBridgeReady(){
                                        WeixinJSBridge.invoke(
                                            'getBrandWCPayRequest', {
                                                "appId":data.result.appId,     //公众号名称，由商户传入
                                                "timeStamp":data.result.timeStamp,         //时间戳，自1970年以来的秒数
                                                "nonceStr":data.result.nonceStr, //随机串
                                                "package":data.result.package,
                                                "signType":"MD5",         //微信签名方式：
                                                "paySign":data.result.paySign //微信签名
                                            },
                                            function(res){
                                                if(res.err_msg == "get_brand_wcpay_request:ok" ){
                                                    location.href = '/home/order/expense.html';
                                                }
                                                if(res.err_msg == "get_brand_wcpay_request:cancel" ){
                                                    location.href = '/home/order/expense.html';
                                                }
                                                if(res.err_msg == "get_brand_wcpay_request:fail" ){
                                                    location.href = '/home/order/expense.html';
                                                }
                                            });
                                    }
                                    if (typeof WeixinJSBridge == "undefined"){
                                        if( document.addEventListener ){
                                            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                                        }else if (document.attachEvent){
                                            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                                            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                                        }
                                    }else{
                                        onBridgeReady();
                                    }
                                }else{
                                    layer.msg(data.msg,{icon:2,time:2000},function(){if(data.url){ window.location.href = data.url;}});
                                }
                            }
                        });
                    });
                }else{
                    layer.msg(result.msg,{icon:2,time:2000},function(){if(result.url){ window.location.href = result.url;}});
                }
            }
        });
    }
</script>
</body>
</html>
